import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/auth/LoginView.vue'
import RegisterView from '../views/auth/RegisterView.vue'
import ProfileView from '../views/ProfileView.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView
    },
    {
      path: '/login-phone',
      name: 'login-phone',
      component: () => import('../views/auth/LoginPhoneView.vue'),
      meta: {
        title: '手机号登录',
        hideTabbar: true
      }
    },
    {
      path: '/register',
      name: 'register',
      component: RegisterView
    },
    {
      path: '/profile',
      name: 'profile',
      component: ProfileView
    },
    {
      path: '/courses',
      name: 'courses',
      component: () => import('../views/course/CoursesView.vue'),
      meta: {
        title: '全部课程'
      }
    },
    {
      path: '/course/:id',
      name: 'courseDetail',
      component: () => import('../views/course/CourseDetailView.vue'),
      meta: {
        requiresAuth: true,
        title: '课程详情'
      }
    },
    {
      path: '/course/:id/learn',
      name: 'courseLearn',
      component: () => import('../views/course/CourseLearnView.vue'),
      meta: {
        requiresAuth: true,
        title: '课程学习'
      }
    },
    {
      path: '/profile/edit',
      name: 'profile-edit',
      component: () => import('../views/profile/EditProfileView.vue')
    },
    {
      path: '/orders',
      name: 'orders',
      component: () => import('../views/orders/OrdersView.vue')
    },
    {
      path: '/favorites',
      name: 'favorites',
      component: () => import('../views/profile/FavoritesView.vue')
    },
    {
      path: '/history',
      name: 'history',
      component: () => import('@/views/history/HistoryView.vue'),
      meta: {
        requiresAuth: true,
        title: '浏览历史'
      }
    },
    {
      path: '/progress',
      name: 'progress',
      component: () => import('../views/profile/ProgressView.vue'),
      meta: {
        requiresAuth: true,
        title: '学习进度'
      }
    },
    {
      path: '/feedback',
      name: 'feedback',
      component: () => import('../views/profile/FeedbackView.vue'),
      meta: {
        requiresAuth: true,
        title: '意见反馈'
      }
    },
    {
      path: '/settings',
      name: 'settings',
      component: () => import('@/views/profile/SettingsView.vue'),
      meta: {
        requiresAuth: true,
        title: '设置'
      }
    },
    {
      path: '/settings/notifications',
      name: 'settings-notifications',
      component: () => import('@/views/settings/NotificationsView.vue'),
      meta: {
        requiresAuth: true,
        title: '消息通知'
      }
    },
    {
      path: '/settings/privacy',
      name: 'settings-privacy',
      component: () => import('@/views/settings/PrivacyView.vue'),
      meta: {
        requiresAuth: true,
        title: '隐私设置'
      }
    },
    {
      path: '/settings/security',
      name: 'settings-security',
      component: () => import('@/views/settings/SecurityView.vue'),
      meta: {
        requiresAuth: true,
        title: '账号安全'
      }
    },
    {
      path: '/settings/terms',
      name: 'settings-terms',
      component: () => import('@/views/settings/TermsView.vue'),
      meta: {
        requiresAuth: true,
        title: '用户协议',
        hideTabbar: true
      }
    },
    {
      path: '/settings/privacy-policy',
      name: 'settings-privacy-policy',
      component: () => import('@/views/settings/PrivacyPolicyView.vue'),
      meta: {
        requiresAuth: true,
        title: '隐私政策',
        hideTabbar: true
      }
    },
    {
      path: '/settings/about',
      name: 'settings-about',
      component: () => import('@/views/settings/AboutView.vue'),
      meta: {
        requiresAuth: true,
        title: '关于我们',
        hideTabbar: true
      }
    },
    {
      path: '/profile/feedback',
      name: 'feedback',
      component: () => import('../views/profile/FeedbackView.vue'),
      meta: {
        requiresAuth: true,
        title: '意见反馈'
      }
    },
    // {
    //   path: '/profile/history',
    //   name: 'history',
    //   component: () => import('../views/profile/HistoryView.vue'),
    //   meta: {
    //     requiresAuth: true,
    //     title: '浏览历史'
    //   }
    // },
    {
      path: '/complaint',
      name: 'complaint',
      component: () => import('@/views/complaint/ComplaintView.vue'),
      meta: {
        title: '投诉建议'
      }
    },
    {
      path: '/apply-teacher',
      name: 'applyTeacher',
      component: () => import('@/views/teacher/ApplyTeacherView.vue'),
      meta: {
        title: '申请成为讲师'
      }
    },
    {
      path: '/video/:id',
      name: 'video',
      component: () => import('@/views/video/VideoView.vue'),
      meta: {
        requiresAuth: true,
        title: '视频播放'
      }
    },
    {
      path: '/article/:id',
      name: 'article',
      component: () => import('@/views/article/ArticleView.vue'),
      meta: {
        requiresAuth: true,
        title: '文章详情'
      }
    },
    {
      path: '/notifications',
      name: 'notifications',
      component: () => import('@/views/notifications/NotificationsView.vue'),
      meta: {
        requiresAuth: true,
        title: '消息通知'
      }
    },
    {
      path: '/featured',
      name: 'featured',
      component: () => import('@/views/featured/FeaturedView.vue'),
      meta: {
        title: '精选内容'
      }
    },
    {
      path: '/order/create',
      name: 'orderCreate',
      component: () => import('../views/orders/CreateOrderView.vue'),
      meta: {
        title: '提交订单',
        requiresAuth: true,
        hideTabbar: true
      }
    },
    {
      path: '/payment/:orderId',
      name: 'payment',
      component: () => import('../views/payment/PaymentView.vue'),
      meta: {
        title: '订单支付',
        requiresAuth: true,
        hideTabbar: true
      }
    },
    {
      path: '/reset-password',
      name: 'reset-password',
      component: () => import('../views/auth/ResetPasswordView.vue'),
      meta: {
        title: '找回密码',
        hideTabbar: true
      }
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  }

  // 暂时关闭登录验证，直接放行所有路由
  next()
  
  // 原登录验证逻辑（暂时注释掉）
  /*
  if (to.meta.requiresAuth) {
    const isAuthenticated = localStorage.getItem('token')
    
    if (!isAuthenticated) {
      next({ name: 'login' })
    } else {
      next()
    }
  } else {
    next()
  }
  */
})

export default router
